﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MadeInPortugal.DataSet1TableAdapters;
using MadeInPortugal.Class;
using System.Collections;

namespace MadeInPortugal
{
    public class EnterpriseUser
    {
        int id;
        string user;
        string password;
        string userType;

        EnterpriseUserTableAdapter EnterpriseUserTable = new EnterpriseUserTableAdapter();

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string User
        {
            get { return user; }
            set { user = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string UserType
        {
            get { return userType; }
            set { userType = value; }
        }

        public EnterpriseUser Insert(EnterpriseUser User)
        {
            short? id = 0;
            EnterpriseUserTable.Insert1(User.User, User.Password,User.UserType, out id);
            User.Id =(int) id;
            User.Password = "";
            return User;
        }
        public EnterpriseUser Update(EnterpriseUser User)
        {
            EnterpriseUserTable.Update(User.User,User.Password,User.UserType,User.Id,User.Id);
            return User;
        }

        public EnterpriseUser SignIn(EnterpriseUser User)
        {
            DataSet1.EnterpriseUserDataTable Datatable = EnterpriseUserTable.GetData(User.User,User.Password);
            foreach (DataSet1.EnterpriseUserRow item in Datatable)
            {
                User.id = item.ID;
                User.UserType = item.UserType;
                User.Password = "";
                User.User = "";
            }
            return User;
        }

        public EnterpriseUser getUserById(int id)
        {
            EnterpriseUser1TableAdapter EnterpriseUserTable1 = new EnterpriseUser1TableAdapter();
            EnterpriseUser User = new EnterpriseUser();
            DataSet1.EnterpriseUser1DataTable Datatable = EnterpriseUserTable1.GetData(id);
            foreach (DataSet1.EnterpriseUser1Row item in Datatable)
            {
                User.id = item.ID;
                User.UserType = item.UserType;
                User.Password = "";
                User.User = item.EnterpriseUser;
            }
            return User;
        }

        public EnterpriseUser getUserByName(string UserName)
        {
            EnterpriseUser1TableAdapter EnterpriseUserTable1 = new EnterpriseUser1TableAdapter();
            EnterpriseUser User = new EnterpriseUser();
            DataSet1.EnterpriseUser1DataTable Datatable = EnterpriseUserTable1.GetDataByName(UserName);
            foreach (DataSet1.EnterpriseUser1Row item in Datatable)
            {
                User.id = item.ID;
                User.UserType = item.UserType;
                User.Password = "";
                User.User = item.EnterpriseUser;
            }
            return User;
        }

        public void Delete(EnterpriseUser User)
        {
            try
            {
                Enterprise enterprise = new Enterprise();
                enterprise.Id = User.id;
                enterprise.Delete(enterprise);
                EnterpriseUserTable.Delete(User.Id, User.User, User.Password);

            }
            catch (Exception)
            {
                
                throw;
            }

            
        }
    }
    public class Enterprise
    {
        int id;
        string name;
        string address;
        string city;
        string postalCode;
        string phone;
        string email;
        string entrepriseName;
        LogoImage logo;
        EnterpriseUser user;

        TBEnterpriseTableAdapter EnterpriseTable = new TBEnterpriseTableAdapter();

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        public string City
        {
            get { return city; }
            set { city = value; }
        }

        public string PostalCode
        {
            get { return postalCode; }
            set { postalCode = value; }
        }
        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        public string EnterpriseName
        {
            get { return entrepriseName; }
            set { entrepriseName = value; }
        }
        public LogoImage Logo
        {
            get { return logo; }
            set { logo = value; }
        }

        public EnterpriseUser User
        {
            get { return user; }
            set { user = value; }
        }

        public Enterprise[] Search(string searchString )
        {
            ArrayList userArray = new ArrayList();

            DataSet1.TBEnterpriseDataTable Datatable = EnterpriseTable.Search(searchString);
            foreach (DataSet1.TBEnterpriseRow item in Datatable)
            {
                Enterprise ent = new Enterprise(); 
                ent.Id = item.ID;
                ent.Address = item.Address;
                ent.City = item.City;
                ent.Email = item.Email;
                ent.EnterpriseName = item.EnterpriseName;
                ent.Name = item.Name;
                ent.Phone = item.Phone;
                ent.PostalCode = item.PostalCode;
                userArray.Add(ent);
            }
            return (Enterprise[])userArray.ToArray(typeof (Enterprise));
        }

        public Enterprise[] GetAllUsers()
        {
            ArrayList userArray = new ArrayList();
            
            DataSet1.TBEnterpriseDataTable Datatable = EnterpriseTable.GetAllUsers();
            foreach (DataSet1.TBEnterpriseRow item in Datatable)
            {
                Enterprise ent = new Enterprise(); 
                ent.Id = item.ID;
                ent.Address = item.Address;
                ent.City = item.City;
                ent.Email = item.Email;
                ent.EnterpriseName = item.EnterpriseName;
                LogoImage image = new LogoImage();
                ent.Logo = image.getImage(item.ID);
                ent.Name = item.Name;
                ent.Phone = item.Phone;
                ent.PostalCode = item.PostalCode;
                ent.User = new EnterpriseUser();
                ent.User.getUserById(item.ID);
                userArray.Add(ent);
            }
            return (Enterprise[])userArray.ToArray(typeof (Enterprise));
        }

        public Enterprise Get(int EnterpriseId)
        {
            Enterprise ret = new Enterprise();
            DataSet1.TBEnterpriseDataTable table= EnterpriseTable.GetData(EnterpriseId);
            foreach (DataSet1.TBEnterpriseRow row in table)
	    {
            LogoImage image = new LogoImage();                
            image = image.getImage(EnterpriseId);
            //EnterpriseUser user = new EnterpriseUser();
            //user = user.
            ret = new Enterprise()
            {
                Address = row.Address,
                City = row.City,
                Email = row.Email,
                EnterpriseName = row.EnterpriseName,
                Id = row.ID,
                Logo = image,
                Name= row.Name,
                Phone = row.Phone,
                PostalCode = row.PostalCode
            };
	    

        }

            return ret;
        }

        public Enterprise GetByEmail(string Email)
        {
            Enterprise ret = new Enterprise();
            DataSet1.TBEnterpriseDataTable table = EnterpriseTable.GetDataByEmail(Email);
            foreach (DataSet1.TBEnterpriseRow row in table)
            {
                ret = new Enterprise()
                {
                    Address = row.Address,
                    City = row.City,
                    Email = row.Email,
                    EnterpriseName = row.EnterpriseName,
                    Id = row.ID,
                    Name = row.Name,
                    Phone = row.Phone,
                    PostalCode = row.PostalCode
                };


            }

            return ret;
        }

        /// <summary>
        /// Create new enterprise user
        /// </summary>
        /// <param name="newEnterprise"></param>
        /// <returns> Enterprise data</returns>
        public Enterprise Insert(Enterprise newEnterprise)
        {
            EnterpriseUser User = new EnterpriseUser();
            newEnterprise.User = User.Insert(newEnterprise.User);
            newEnterprise.Id = newEnterprise.User.Id;

            EnterpriseTable.Insert(
                newEnterprise.Id,
                newEnterprise.Name,
                newEnterprise.Address,
                newEnterprise.City,
                newEnterprise.PostalCode,
                newEnterprise.Phone,
                newEnterprise.Email,
                newEnterprise.EnterpriseName
                );

            if (newEnterprise.Logo != null)
            {
                newEnterprise.Logo.Id = newEnterprise.Id;
                newEnterprise.Logo.insertLogo(newEnterprise.Logo.Logo, newEnterprise.Logo.Description, newEnterprise.Id);
            }

            return newEnterprise;
        }

        public Enterprise Update(Enterprise newEnterprise)
        {
            EnterpriseTable.Update(
                newEnterprise.Name,
                newEnterprise.Address,
                newEnterprise.City,
                newEnterprise.PostalCode,
                newEnterprise.Phone,
                newEnterprise.Email,
                newEnterprise.EnterpriseName,
                newEnterprise.Id);
            if (newEnterprise.Logo != null)
            {
                try
                {
                    newEnterprise.Logo.Id = newEnterprise.Id;
                    newEnterprise.Logo.deleteLogo(newEnterprise.Logo.Id);
                    newEnterprise.Logo.insertLogo(newEnterprise.Logo.Logo, newEnterprise.Logo.Description, newEnterprise.Logo.Id);
                }
                catch (Exception)
                {

                    //logo.updateLogo(newEnterprise.Logo.Logo, newEnterprise.Logo.Description, newEnterprise.Id);
                }
                
            }
            return newEnterprise;
        }

        public void Delete(Enterprise newEnterprise)
        {
            try
            {
                EnterpriseTable.Delete(newEnterprise.Id);
                logo.deleteLogo(newEnterprise.Id);
            }
            catch (Exception)
            {
            }
        }
    }
    public class LogoImage
    {
        int id;
        byte[] logo;
        string description;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public byte[] Logo
        {
            get { return logo; }
            set { logo = value; }
        }
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public LogoImage getImage(int ImageId)
        {
            LogoImage ret = new LogoImage();
            TBLogoTableAdapter imageTableAdapter = new TBLogoTableAdapter();
            DataSet1.TBLogoDataTable imageTable = imageTableAdapter.GetData(ImageId);
            foreach (DataSet1.TBLogoRow imageRow in imageTable)
            {

                ret.Id = imageRow.Id;
                ret.Logo = imageRow.logo;
                ret.Description = imageRow.description;
            }
            return ret;

        }

        public LogoImage insertLogo(byte[] image, string description, int ImageId)
        {
            TBLogoTableAdapter imageTableAdapter = new TBLogoTableAdapter();
            imageTableAdapter.Insert(image, description, id);
            return new LogoImage() { Logo = image, Description = description, Id = ImageId };

        }

        public LogoImage updateLogo(byte[] image, string description, int ImageId)
        {
            TBLogoTableAdapter imageTableAdapter = new TBLogoTableAdapter();
            imageTableAdapter.Update(image, description, id);
            return new LogoImage() { Logo = image, Description = description, Id = ImageId };

        }

        public bool deleteLogo(int LogoId)
        {
            bool ret = false;
            TBLogoTableAdapter imageTableAdapter = new TBLogoTableAdapter();
            try
            {
                imageTableAdapter.Delete(LogoId);
            }
            catch (Exception)
            {

                ret = true;
            }

            return ret;

        }
    }
}
